#include <gtest/gtest.h>
#include <market_data/market_data_monitor.h>
#include <chrono>
#include <thread>

using namespace hft;
using namespace std::chrono;

class MarketDataMonitorTest : public ::testing::Test {
protected:
    void SetUp() override {
        monitor = std::make_unique<MarketDataMonitor>();
        monitor->setCheckInterval(std::chrono::milliseconds(100));
        monitor->setMaxDelay(std::chrono::milliseconds(500));
        monitor->setMaxErrorRate(0.01);
        monitor->setMinMessageRate(1.0);
    }

    void TearDown() override {
        monitor.reset();
    }

    std::unique_ptr<MarketDataMonitor> monitor;
};

TEST_F(MarketDataMonitorTest, InitialState) {
    EXPECT_FALSE(monitor->isAbnormal());
    EXPECT_EQ(monitor->getErrorCount(), 0);
    EXPECT_EQ(monitor->getMessageCount(), 0);
}

TEST_F(MarketDataMonitorTest, MessageDelay) {
    auto now = system_clock::now();
    auto delayed = now - milliseconds(600);  // 超过最大延迟
    
    monitor->onMarketData("IF2309", delayed);
    EXPECT_TRUE(monitor->isAbnormal());
}

TEST_F(MarketDataMonitorTest, ErrorRate) {
    // 模拟100条消息，其中2条错误（超过1%阈值）
    for (int i = 0; i < 98; ++i) {
        monitor->onMarketData("IF2309", system_clock::now());
    }
    
    auto delayed = system_clock::now() - milliseconds(600);
    monitor->onMarketData("IF2309", delayed);
    monitor->onMarketData("IF2309", delayed);
    
    EXPECT_TRUE(monitor->isAbnormal());
    EXPECT_EQ(monitor->getErrorCount(), 2);
    EXPECT_EQ(monitor->getMessageCount(), 100);
}

TEST_F(MarketDataMonitorTest, MessageRate) {
    // 测试消息率
    auto start = system_clock::now();
    
    // 发送5条消息
    for (int i = 0; i < 5; ++i) {
        monitor->onMarketData("IF2309", system_clock::now());
        std::this_thread::sleep_for(milliseconds(100));
    }
    
    auto elapsed = duration_cast<seconds>(system_clock::now() - start);
    double rate = monitor->getMessageRate();
    
    // 预期消息率应该在10左右（5条消息/0.5秒）
    EXPECT_GE(rate, 8.0);
    EXPECT_LE(rate, 12.0);
}

TEST_F(MarketDataMonitorTest, Reset) {
    // 先产生一些错误数据
    auto delayed = system_clock::now() - milliseconds(600);
    monitor->onMarketData("IF2309", delayed);
    monitor->onMarketData("IF2309", delayed);
    
    EXPECT_TRUE(monitor->isAbnormal());
    EXPECT_GT(monitor->getErrorCount(), 0);
    
    // 重置监控器
    monitor->reset();
    
    EXPECT_FALSE(monitor->isAbnormal());
    EXPECT_EQ(monitor->getErrorCount(), 0);
    EXPECT_EQ(monitor->getMessageCount(), 0);
}

TEST_F(MarketDataMonitorTest, MultipleSymbols) {
    // 测试多个合约的数据监控
    monitor->onMarketData("IF2309", system_clock::now());
    monitor->onMarketData("IC2309", system_clock::now());
    monitor->onMarketData("IH2309", system_clock::now());
    
    EXPECT_EQ(monitor->getMessageCount(), 3);
    EXPECT_FALSE(monitor->isAbnormal());
}

TEST_F(MarketDataMonitorTest, StatusCallback) {
    bool callbackCalled = false;
    bool statusValue = false;
    
    monitor->setStatusCallback([&](bool status) {
        callbackCalled = true;
        statusValue = status;
    });
    
    // 触发异常状态
    auto delayed = system_clock::now() - milliseconds(600);
    monitor->onMarketData("IF2309", delayed);
    
    EXPECT_TRUE(callbackCalled);
    EXPECT_TRUE(statusValue);  // 应该报告异常状态
}